package faircat.android.exphone;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.TreeMap;
import java.util.Comparator;
import java.util.List;
import android.database.Cursor;
import android.provider.CallLog;
import android.content.Context;
import android.os.Handler;

public class CallFilterDataSet {
	
	public class PhoneIndex implements Comparable<PhoneIndex>{
		String mPhone;
		int mSlot;
		PhoneIndex(String phone, int slot){
			mPhone = phone;
			mSlot = slot;
		}
		
		public int compareTo(PhoneIndex param){
			if( mSlot != param.mSlot ){
				return -1;
			}
			return param.mPhone.compareTo(mPhone);
		}
	};
	
	public TreeMap<PhoneIndex, List<Call>> mCalls = new TreeMap<PhoneIndex, List<Call>>();
	
	Cursor mMainCalls = null;
	Cursor mSlot2Calls = null;
	
	Context mContext = null;
	
	ExCallLog mExCallLog = null;
	
	static final String[] CALL_PROJ= new String[]{
		"_id",
		"number",
		"date",
		"type",
		"duration",
	};
	
	public class Call implements Comparable<Call>{
		public long mId;
		public int mSlot;
		public long mDate;
		public int mDuration;
		public int mType;
		public String mNumber;
		public String mRawNuber;
		
		public int compareTo(Call param){
			return Long.valueOf(param.mDate).compareTo(mDate);
		}
		
		public Call(long id, int slot, long date, int duration, int type, String number){
			mId = id;
			mSlot = slot;
			mDate = date;
			mDuration = duration;
			mType = type;
			mNumber = number;
		}
	}
	
	public static final Comparator<String> StringComparator = new Comparator<String>(){
		public int compare(String from, String to){
			return from.compareTo(to);
		}
	};
	
	public CallFilterDataSet(Context ctx){
		mContext = ctx;
		mExCallLog = new ExCallLog(ctx);
	}
	
	public void close(){
		mExCallLog.close();
	}
	
	public void queryAll(Handler handler){
		mMainCalls = mContext.getContentResolver().query(
				CallLog.Calls.CONTENT_URI, 
				CALL_PROJ, 
				null, 
				null, 
				CallLog.Calls.DATE + " desc");
		mSlot2Calls = mExCallLog.queryAll();
		int idIndex1 = mMainCalls.getColumnIndex("_id");
		int idIndex2 = mSlot2Calls.getColumnIndex("_id");
		int dateIndex1 = mMainCalls.getColumnIndex("date");
		int dateIndex2 = mSlot2Calls.getColumnIndex("date");
		int numberIndex1= mMainCalls.getColumnIndex("number");
		int numberIndex2 = mSlot2Calls.getColumnIndex("number");
		int typeIndex1 = mMainCalls.getColumnIndex("type");
		int typeIndex2 = mSlot2Calls.getColumnIndex("type");
		int durationIndex1 = mMainCalls.getColumnIndex("duration");
		int durationIndex2 = mSlot2Calls.getColumnIndex("duration");
		long date1 = -1;
		long date2 = -1;
		if( mMainCalls.moveToFirst() ){
			date1 = mMainCalls.getLong(dateIndex1);
		}
		if( mSlot2Calls.moveToFirst() ){
			date2 = mSlot2Calls.getLong(dateIndex2);
		}
		long date = 0;
		if( date1 < date2 ){
			date = date1;
		}
		else{
			date = date2;
		}
		while(date1 != -1 || date2 != -1){
			if( date1 >= date ){
				String phone = mMainCalls.getString(numberIndex1);
				PhoneIndex key = new PhoneIndex(phone, 0);
				
				List<Call> list = mCalls.get(key);
				
				if( list == null ){
					list = new ArrayList<Call>();
					mCalls.put(key, list);
				}
				list.add(new Call(
						mMainCalls.getInt(idIndex1),
						0,
						date1,
						mMainCalls.getInt(durationIndex1),
						mMainCalls.getInt(typeIndex1),
						phone
						));
				handler.obtainMessage(0, list.size(), 0, key);
				
				if( mMainCalls.moveToNext() ){
					date1 = mMainCalls.getLong(dateIndex1);
					if( date1 < date ){
						date = date1;
					}
				}
				else{
					date1 = -1;
					date = 0;
				}
			}
			else if( date2 >= date ){
				String phone = mSlot2Calls.getString(numberIndex2);
				PhoneIndex key = new PhoneIndex(phone, 1);
				List<Call> list = mCalls.get(key);
				if( list == null ){
					list = new ArrayList<Call>();
					mCalls.put(key, list);
				}
				list.add(new Call(
						mSlot2Calls.getInt(idIndex2),
						1,
						date2,
						mSlot2Calls.getInt(durationIndex2),
						mSlot2Calls.getInt(typeIndex2),
						phone
						));
				handler.obtainMessage(0, list.size(), 0, key);
				if( mSlot2Calls.moveToNext() ){
					date2 = mSlot2Calls.getLong(dateIndex2);
					if( date2 < date ){
						date = date2;
					}
				}
				else{
					date2 = -1;
					date = 0;
				}
			}
		}
	}
}
